<?php
//include("includes/connect.php");
//include("includes/function.php");

$petition_id = $_REQUEST["petition_id"];
$db = new myDBS;

//$where = "petition_id = '$petition_id'";
//$result = $db->showData1("tbl_petition", $where);
$sql = "SELECT tbl_petition.*, expenses1_name, expenses2_name, expenses3_name FROM tbl_petition, expenses_lvl1, expenses_lvl2, expenses_lvl3 WHERE petition_id  = '$petition_id' and expenses_lvl1.expenses1_id = ex1_id and expenses_lvl2.expenses2_id = ex2_id and expenses_lvl3.expenses3_id = ex3_id";
//echo $sql;
$result = $db->runSQL($sql);

$rows = $db->fetch_object($result);
?>

<script type="text/javascript">
    $(document).ready(function() {

        $.fn.myFunction = function() {
            var inpValues = $("#inpValues").val();
            var WithholdingTax = $("#WithholdingTax").val();
            var inpPenalty = $("#inpPenalty").val();

            inpValues = parseFloat(inpValues);
            WithholdingTax = parseFloat(WithholdingTax);

            WithholdingTax = inpValues * WithholdingTax / 100;
            $("#sHoldingTax").html(WithholdingTax.toFixed(2));

            inpNet = inpValues - WithholdingTax - inpPenalty;
            $("#inpNet").val(inpNet.toFixed(2));
        }

        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
            submitSuccess: function($form, event) {
                event.preventDefault();
                //var ppID = $(#ppID)

                $.ajax({
                    type: "POST",
                    url: "processPetition.php",
                    data: $('#frmPetition').serialize(),
                    cach: false,
                    error: function() {
                        alert("ERROR !!!!");
                    },
                    success: function(data) {
                        //alert(data);
                        //window.location.href="index.php";
                        //$('#myTab a[href="#profile"]').tab('show');
                        //$("#kob").after(data);
                        //$("#dispStatus").html(data);
                        window.location.href = "index.php?pages=sumPetition&petition_id=" + data;

                    }
                });

            }
        });

        $("#treasury_date").datepicker({
            showButtonPanel: true
        });

        $("#add_payfor").click(function() {
            //event.preventDefault();
            //alert("asdfas");
            var dd_payfor = $("#dd_payfor").val();
            var xxx_id = $("#h_key").val();
            var inpAmount = $("#inpAmount").val();
            var task = "insert";

            $.ajax({
                type: "POST",
                url: "payfor.php",
                data: {'task': task, 'payfor': dd_payfor, 'inpAmount': inpAmount, 'xxx_id': xxx_id},
                cach: false,
                error: function() {
                    alert("ERROR !!!!");
                },
                success: function(data) {
                    if (data) {
                        $("#kob").html(data);
                    }
                }
            });

        });

        $("#inpTax").keyup(function() {
            var inpValues = $("#inpValues").val();
            var inpTax = $("#inpTax").val();

            inpValues = parseFloat(inpValues);

            inpTax = parseFloat(inpTax);

            inpTax = inpValues - ((inpValues * 100) / (100 + inpTax));

            //inpTax = inpTax.toFixed(2);
            $("#stax").html(inpTax.toFixed(2));

        });

        $("#inpTax").change(function() {
            var inpValues = $("#inpValues").val();
            var inpTax = $("#inpTax").val();

            //alert("sadf");
            inpValues = parseFloat(inpValues);

            inpTax = parseFloat(inpTax);

            inpTax = inpValues - ((inpValues * 100) / (100 + inpTax));

            //inpTax = inpTax.toFixed(2);
            $("#stax").html(inpTax.toFixed(2));

        });

        $("#WithholdingTax").keyup(function() {
            $.fn.myFunction();

        });

        $("#WithholdingTax").change(function() {
            $.fn.myFunction();
        });

        $("#inpPenalty").keyup(function() {
            $.fn.myFunction();

        });

        $("#inpPenalty").change(function() {
            $.fn.myFunction();

        });

        $("#inpValues").keyup(function() {
            /*
             //alert(event.which);
             if((event.which >= 48 && event.which <= 57) || (event.which >= 96 && event.which <= 105) || event.which == 110 || (event.which >= 37 && event.which <= 40) || event.which==8 || event.which==46) {
             //alert("asdf");
             }
             /*
             } else {
             event.preventDefault();
             }
             */
        });


    }); //(document).ready

    function del_temp(id) {
        var task = "delete";
        //alert(id);
        if (confirm('คุณแน่ใจที่จะลบรายการนี้หรือไม่? ')) {
            $.post('payfor.php', {task: task, id: id}, function(data) {
                $("#kob").html(data);
            });
        }
    }


</script>

<div id="dispStatus"></div>
<form id="frmPetition" name="frmPetition" method="post" action="#" novalidate="">
    <div class="row-fluid">
        <div class="control-group span7">
            <label class="control-label">ส่วนราชการ</label>
            <div class="controls">
                                <!--<span class="span7 uneditable-input">เทศบาลอุดรธานี</span>-->
                <input class="span7 uneditable-input" type="text" id="lvl1_name" name="lvl1_name" value="<?php echo $rows->lvl1_name; ?>" readonly />
            </div>                
        </div>
        <div class="control-group span5">
            <label class="control-label">หน่วย</label>
            <div class="controls">
                <input class="span5 uneditable-input" type="text" id="lvl2_name" name="lvl2_name" value="<?php echo $rows->lvl2_name; ?>" readonly />
            </div>                
        </div>
    </div>

    <div class="row-fluid">
        <div class="control-group span2">
            <label class="control-label">ปีงบประมาณ</label>
            <div class="controls">
                <input class="span2 uneditable-input" type="text" id="budget_year" name="budget_year" value="<?php echo $rows->budget_year; ?>" readonly />
            </div>                
        </div>
        <div class="control-group span3">
            <label for="dob" class="control-label">เลขที่ผู้เบิก</label>
            <div class="controls">
                <input class="span12" type="text" id="p_id" name="p_id" value="<?php echo $rows->p_id; ?>" readonly />
            </div>
        </div>
        <div class="control-group span3">
            <label for="dob" class="control-label">เลขที่คลังรับ</label>
            <div class="controls">
                <input class="span12" type="text" id="treasury_id" name="treasury_id" value="" readonly />
            </div>
        </div>
        <div class="control-group  span3">
            <label for="dob" class="control-label">วันที่คลังรับ</label>
            <div class="controls">
                <input class="span12" type="text" id="treasury_date" name="treasury_date" readonly/>
            </div>
        </div>
    </div>

    <div class="row-fluid">
        <div class="control-group span4">
            <label class="control-label">แผนงาน</label>
            <div class="controls">
                <input class="span12" type="text" id="panngan2_name" name="panngan2_name" value="<?php echo $rows->panngan2_name; ?>" readonly />
            </div>                
        </div>	
        <div class="control-group span4">
            <label class="control-label">งาน</label>
            <div class="controls">
                <input class="span12" type="text" id="panngan3_name" name="panngan3_name" value="<?php echo $rows->panngan3_name; ?>" readonly />
            </div>                
        </div>
        <div class="control-group span2">
            <label class="control-label">รหัสบัญชี</label>
            <div class="controls">
                <input class="span12" type="text" id="acc_id" name="acc_id" value="<?php echo $rows->acc_id; ?>"  />
            </div>                
        </div>
        <div class="control-group span2">
            <label class="control-label">E-lass</label>
            <div class="controls">
                <input class="span12" type="text" id="elass" name="elass" value="<?php echo $rows->elass; ?>"  />
            </div>                
        </div>
    </div>

    <div class="row-fluid">
        <div class="control-group span4">
            <label class="control-label">หมวด / ลักษณะ</label>
            <span class="span4 uneditable-input"><?php echo $rows->expenses1_name; ?></span>
                        <!--<input type="hidden" id="ex1_id" name="ex1_id" value="<?php echo $rows->ex1_id; ?>"  />-->
        </div>
        <div class="control-group span4">
            <label class="control-label">ประเภท</label>
            <span class="span4 uneditable-input"><?php echo $rows->expenses2_name; ?></span>
            <!--
<div id="expend_sub_type" class="controls">
                    <select class="span12" id="dd_expend_sub_type" name="dd_expend_sub_type">
                            <option><?php echo $rows->dd_expend_sub_type; ?></option>
                     </select>
</div>                
            -->
        </div>
        <div class="control-group span4">
            <label class="control-label">รายการ</label>
            <span class="span4 uneditable-input"><?php echo $rows->expenses3_name; ?></span>
            <!--
<div id="expend_mini_type" class="controls">
                    <select class="span12"  id="dd_expend_mini_type" name="dd_expend_mini_type">
                      <option><?php echo $rows->dd_expend_mini_type; ?></option>
                     </select>
</div>                
            -->
        </div>
    </div>
    <div class="row-fluid">

        <div class="form-horizontal span5">
            <div class="control-group">
                <label class="control-label" for="inpValues">มูลค่าสินค้า / บริการ</label>
                <div class="controls">
                    <input type="number" id="inpValues" name="inpValues" placeholder="มูลค่าสินค้า / บริการ" value="<?php echo $rows->inpValues; ?>"  readonly>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inpTax">บวก ภาษีมูลค่าเพิ่ม (%)</label>
                <div class="controls">
                    <input class="span2" type="number" id="inpTax" name="inpTax" placeholder="ภาษีมูลค่าเพิ่ม" value="<?php echo $rows->inpTax; ?>" ><span id="stax" class="span2 uneditable-input text-right"></span>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inpTotal">จำนวนเงินที่ขอเบิกทั้งสิ้น</label>
                <div class="controls">
                    <input type="text" id="inpTotal" class="text-right" name="inpTotal" placeholder="จำนวนเงินที่ขอเบิกทั้งสิ้น" value="<?php echo $rows->inpTotal; ?>" readonly>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="WithholdingTax">หัก ภาษี ณ ที่จ่าย</label>
                <div class="controls">
                    <input class="span2 text-right" type="number" id="WithholdingTax" name="WithholdingTax" placeholder="ภาษี ณ ที่จ่าย" value="<?php echo $rows->WithholdingTax; ?>" ><span id="sHoldingTax" class="span2 uneditable-input text-right"></span>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inpPenalty">ค่าปรับ</label>
                <div class="controls">
                    <input type="number" class="text-right" id="inpPenalty" name="inpPenalty" placeholder="ค่าปรับ" value="<?php echo $rows->inpPenalty; ?>">
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inpNet">จำนวนเงินที่่รับสุทธิ</label>
                <div class="controls">
                    <input type="text" id="inpNet" class="text-right" name="inpNet" placeholder="จำนวนเงินที่่รับสุทธิ" value="<?php echo $rows->inpNet; ?>" readonly>
                </div>
            </div>
        </div> <!-- <div class="form-horizontal span5"> -->


        <div class="span7">
            <div class="row-fluid">
                <div id="kob">
                    <table class="table table-bordered" width="100%">
                        <thead>
                            <tr bgcolor="#e5e5e5">
                                <th>ชื่อ - นามสกุล / บริษัท</th>

                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td><?php echo $rows->company_name; ?>&nbsp;</td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <label>หมายเหตุ</label>
                <textarea class="span12" rows="9" id="comment" name="comment"><?php echo $rows->comment; ?></textarea>
            </div>

        </div><!-- <div class="span8"> -->

    </div>

    <input type="hidden" id="company_name" name="company_name" value="<?php echo $rows->company_name; ?>">
    <input type="hidden" id="ppID" name="ppID" value="<?php echo $ppID; ?>">
    <input type="hidden" id="task" name="task" value="edit">
    <input type="hidden" id="petition_id" name="petition_id" value="<?php echo $petition_id; ?>">
    <div id="key">
        <input type="hidden" id="h_key" name="h_key" value="">
    </div>
    <p class="pull-right">

        <button type="submit" id="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> บันทึกข้อมูล</button>
        <a href="index.php?pages=home" role="button" class="btn"><i class="icon-remove"></i>ยกเลิก</a>
    </p>

</form> 

<script type="text/javascript">
    $(document).ready(function() {
        $('#inpTax').trigger('change');
        $('#WithholdingTax').trigger('change');
    });


</script>